package org.budo.dubbo.monitor.web.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.budo.dubbo.monitor.entity.DubboInvoke;
import org.budo.dubbo.monitor.entity.DubboInvokeLineChart;
import org.budo.dubbo.monitor.entity.LineChartSeries;
import org.budo.dubbo.monitor.util.ResponseEntity;
import org.budo.dubbo.monitor.web.service.IDubboInvokeService;
import org.budo.time.Time;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author limingwei2
 */
@Controller
public class IndexController {
    @Resource
    IDubboInvokeService dubboInvokeService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home() {
        return "index";
    }

    @ResponseBody
    @RequestMapping("now")
    public String now() {
        return "budo-dubbo-monitor "+Time.now().toString("yyyy-MM-dd HH:mm:ss.SSS");
    }

    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index() {
        return "index";
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @ResponseBody
    @RequestMapping(value = "load_top_data")
    public ModelAndView loadTopDate(@ModelAttribute DubboInvoke dubboInvoke) {
        ResponseEntity commonResponse = ResponseEntity.createCommonResponse();
        List<DubboInvokeLineChart> dubboInvokeLineChartList = new ArrayList<DubboInvokeLineChart>();
        DubboInvokeLineChart successDubboInvokeLineChart = new DubboInvokeLineChart();
        List<String> sxAxisCategories = new ArrayList<String>();
        LineChartSeries slineChartSeries = new LineChartSeries();
        List<double[]> sdataList = new ArrayList<double[]>();
        double[] data;
        Map dubboInvokeMap = dubboInvokeService.countDubboInvokeTopTen(dubboInvoke);
        List<DubboInvoke> success = (List<DubboInvoke>) dubboInvokeMap.get("success");
        for (DubboInvoke di : success) {
            sxAxisCategories.add(di.getMethod());
            data = new double[] { di.getSuccess() };
            sdataList.add(data);
        }
        slineChartSeries.setData(sdataList);
        slineChartSeries.setName("provider");

        successDubboInvokeLineChart.setxAxisCategories(sxAxisCategories);
        successDubboInvokeLineChart.setSeriesData(Arrays.asList(slineChartSeries));
        successDubboInvokeLineChart.setChartType("SUCCESS");
        successDubboInvokeLineChart.setTitle("The Top 20 of Invoke Success");
        successDubboInvokeLineChart.setyAxisTitle("t");
        dubboInvokeLineChartList.add(successDubboInvokeLineChart);

        DubboInvokeLineChart failureDubboInvokeLineChart = new DubboInvokeLineChart();
        List<String> fxAxisCategories = new ArrayList<String>();
        LineChartSeries flineChartSeries = new LineChartSeries();
        List<double[]> fdataList = new ArrayList<double[]>();
        List<DubboInvoke> failure = (List<DubboInvoke>) dubboInvokeMap.get("failure");
        for (DubboInvoke di : failure) {
            fxAxisCategories.add(di.getMethod());
            data = new double[] { di.getFailure() };
            fdataList.add(data);
        }
        flineChartSeries.setData(fdataList);
        flineChartSeries.setName("provider");

        failureDubboInvokeLineChart.setxAxisCategories(fxAxisCategories);
        failureDubboInvokeLineChart.setSeriesData(Arrays.asList(flineChartSeries));
        failureDubboInvokeLineChart.setChartType("FAILURE");
        failureDubboInvokeLineChart.setTitle("The Top 20 of Invoke Failure");
        failureDubboInvokeLineChart.setyAxisTitle("t");
        dubboInvokeLineChartList.add(failureDubboInvokeLineChart);

        commonResponse.setData(dubboInvokeLineChartList);
        return new ModelAndView().addAllObjects(commonResponse);
    }
}